package com.highspeed.bdk.service.deploy;

import com.highspeed.bdk.bean.entity.deploy.DeployTopology;
import com.highspeed.bdk.bean.entity.logic.App;
import com.highspeed.bdk.bean.entity.logic.LogicTopology;
import com.highspeed.bdk.bean.entity.physical.PhysicalLink;
import com.highspeed.bdk.bean.entity.physical.PhysicalTopology;
import com.highspeed.bdk.repository.deploy.DeployTopologyRepository;
import com.highspeed.bdk.service.base.BdkBaseService;
import com.highspeed.bdk.service.logic.ILogicTopologyService;
import com.highspeed.bdk.service.physical.IPhysicalLinkService;
import com.highspeed.bdk.utils.PhysicalDeviceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by chenjiabin on 2018/3/23.
 */
@Service
@Transactional
public class DeployTopologyService extends BdkBaseService<DeployTopology, Integer> implements IDeployTopologyService {
    @Autowired
    private DeployTopologyRepository deployTopologyRepository;
    @Autowired
    private ILogicTopologyService logicTopologyService;

    @Override
    public DeployTopologyRepository getRepository() {
        return deployTopologyRepository;
    }

    @Override
    public void delete(Integer[] ids) {
        if (ids != null) {
            List<DeployTopology> tps = new ArrayList<DeployTopology>(0);
            for (Integer id : ids) {
                DeployTopology old = findOne(id);
                if (old != null) {
                    old.setEnabled(false);
                }
                tps.add(old);
            }
            save(tps);
        }
    }

    @Override
    public <S extends DeployTopology> S save(S entity) {

        initDeployTopology(entity);
        return super.save(entity);
    }

    @Override
    public <S extends DeployTopology> List<S> save(Iterable<S> entities) {
        if (entities != null) {
            for (S mb : entities) {
                initDeployTopology(mb);
            }
        }
        return super.save(entities);
    }

    private void initDeployTopology(DeployTopology deployTopology) {
        if (deployTopology.getCreator() == null) {
            deployTopology.setCreator(SecurityContextHolder.getContext().getAuthentication().getName());
        }
    }

    @Override
    public <S extends DeployTopology> S saveAndFlush(S entity) {
        initDeployTopology(entity);
        return super.saveAndFlush(entity);
    }

    @Override
    public void delete(Integer id) {
        super.delete(new Integer[]{id});
    }

    @Override
    public List<Map<String, Integer>> queryVirtualLinksByDeployTopologyId(Integer id) {
        DeployTopology deployTopology = findOne(id);
        if (deployTopology != null) {
            return logicTopologyService.queryVirtualLinksByLogicTopologyId(deployTopology.getId());
        } else {
            return new ArrayList<Map<String, Integer>>(0);
        }
    }
}
